package Utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.faces.context.FacesContext;

import sun.misc.BASE64Encoder;

public class Uteis {	
	
	public static java.sql.Date javaDate2SqlDate(Date data) throws ParseException{
	    java.sql.Date dataSql = new java.sql.Date(data.getTime());
		return dataSql;
	}

	public static java.sql.Time javaString2SqlTime(String hora) throws ParseException
	{
		SimpleDateFormat  df = new SimpleDateFormat("HH:mm");  
		Date dataFormatada = df.parse(hora);		
		java.sql.Time timeSql = new java.sql.Time(dataFormatada.getTime());
		return timeSql;
	}
	
	public static java.util.Date string2UtilDate(String data) throws ParseException
	{
		SimpleDateFormat  df = new SimpleDateFormat("dd/MM/yyyy");  
		Date dataFormatada = df.parse(data);		
		return dataFormatada;	
	}
	
	public static String formatarData(java.sql.Date data)
	{
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		return df.format(data);		
	}
	
	public static boolean parseInt2Boolean(int value)
	{
		return value == 1 ? true : false;
	}	
	
    public static String encripta (String senha) {     
        try {     
             MessageDigest digest = MessageDigest.getInstance("MD5");      
             digest.update(senha.getBytes());      
             BASE64Encoder encoder = new BASE64Encoder ();      
             return encoder.encode (digest.digest ());      
        } catch (NoSuchAlgorithmException ns) {     
             ns.printStackTrace ();      
             return senha;      
        }      
   } 
    
    
public static boolean validaCPF(String stringCPF)
{    
	
	if(stringCPF.contains(".") &&stringCPF.contains("-"))
		stringCPF = stringCPF.replace(".", "").replace("-", ""); 
	 
	
	int i, soma1, soma2, digito1, digito2;
    if(stringCPF.length() != 11)
      return false;

    if( (stringCPF.equals("00000000000")) || (stringCPF.equals("11111111111")) ||
        (stringCPF.equals("22222222222")) || (stringCPF.equals("33333333333")) ||
        (stringCPF.equals("44444444444")) || (stringCPF.equals("55555555555")) ||
        (stringCPF.equals("66666666666")) || (stringCPF.equals("77777777777")) ||
        (stringCPF.equals("88888888888")) || (stringCPF.equals("99999999999")))
      return false;

    //Calcula o primeiro dígito
    soma1 = 0;
    for(i=0; i<=8; i++)
      soma1 = soma1 + Integer.parseInt(stringCPF.substring(i, i+1)) * (10 - i);

    if(soma1 % 11 < 2)
        digito1 = 0;
    else
        digito1 = 11 - (soma1 % 11);

    //Calcula o segundo dígito
    soma2 = 0;
    for(i=0; i<=9; i++)
        soma2 = soma2 + Integer.parseInt(stringCPF.substring(i, i+1)) * (11 - i);

    if(soma2 % 11 < 2)
      digito2 = 0;
    else
      digito2 = 11 - (soma2 % 11);

    if( (digito1 == Integer.parseInt(stringCPF.substring(9, 10))) && (digito2 == Integer.parseInt(stringCPF.substring(10))) )
        return true;

    return false;

}
    
	
}
